﻿using LogLib;
using System;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using ComLib.ConfigService;
using ComLib;
using ProcessConfig;

namespace ConfigManager
{
    /// <summary>
    /// 应用程序配置类
    /// </summary>
    public static class ConfigManage
    {
        /// <summary>
        /// 错误信息
        /// </summary>
        private static string _errorMessage;
        public static string ErrorMessage
        {
            get { return _errorMessage; }
        }

        private static ConfigService _service;
        public static ConfigService Service
        {
            get { return _service; }
        }

        public static void Initialize()
        {
            InitConfigData();
            CheckFolder();               
        }

        public static void Initialize(ConfigService s)
        {
            _service = s;
        }

        public static string HelpPath()
        {
            return IniConfigFile.IniValue("Config", "helpPath", "");
        }

        public static void InitConfigData()
        {
            _errorMessage = "";
            try
            {
                _service = ConfigService.GetInstance();
                _service.LoadConfig();
            }
            catch (Exception ex)
            {
                _errorMessage = ex.Message;
                Log.WriteException(ex.ToString());
            }
        }

        delegate void CheckDelegate(string s);
        public static void CheckFolder()
        {
            CheckDelegate delFun = (string path) =>
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            };
            //delFun(Configs.LogUsage.UserRecordPath);
        }

        #region Get Functions
        public static DateTime GetAssemblyVersionTime()
        {
            Assembly assembly = Assembly.LoadFrom(Application.ExecutablePath);
            var version = Assembly.LoadFrom(Application.ExecutablePath).GetName().Version;
            return new DateTime(2000, 1, 1).AddDays(version.Build).AddSeconds(version.Revision * 2);
        }
        #endregion
     
        /// <summary>
        /// 保存设置
        /// </summary>
        public static bool SaveSettings()
        {
            _errorMessage = "";
            try
            {              
                _service.SaveConfig();
                return true;
            }
            catch (System.Exception ex)
            {
                Log.WriteException(ex.ToString());
                return false;
            }
        }

        #region Set Functions

        //   long ll = IniConfigFile.WriteValue("EMP2930", "SerialPortNum", strPort, strFile);

        #endregion

    };
}
